from PIL import Image

from strange_io.pixels import StrangePixels
from strange_io.colors import StrangeColors

def convert(spixels: StrangePixels, scolors: StrangeColors, transperant: bool) -> Image.Image:
    """ Convert document to PIL image """

    if transperant:
        img = Image.new('RGBA', (spixels.width, spixels.height))
        for x in range(spixels.width):
            for y in range(spixels.height):
                alpha = 255
                if spixels.image[x][y] == scolors.transperent_index:
                    alpha = 0
                img.putpixel((x, y), tuple([*scolors.colors[spixels.image[x][y]], alpha]))
    else:
        img = Image.new('RGB', (spixels.width, spixels.height))
        for x in range(spixels.width):
            for y in range(spixels.height):
                img.putpixel((x, y), scolors.colors[spixels.image[x][y]])
    return img
